package org.csu.equipment.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.csu.equipment.entity.*;
import org.csu.equipment.persistence.*;
import org.csu.equipment.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

@Service
public class EmployeeServiceImpl implements EmployeeService {
    @Autowired
    private EmployeeLoginMapper employeeLoginMapper;

    @Autowired
    private TaskMapper taskMapper;

    @Autowired
    private EmployeeSkillMapper employeeSkillMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private WarehouseMapper warehouseMapper;
    @Autowired
    private RecordMapper recordMapper;

    @Override
    public Integer login(String EId, String EPwd) {
        QueryWrapper<EmployeeLogin> queryWrapper = new QueryWrapper<EmployeeLogin>();
        queryWrapper.eq("E_id",EId).eq("E_pwd",EPwd);
        Integer count = employeeLoginMapper.selectCount(queryWrapper);
        return count;
    }

    @Override
    public void finishTask(String TId, String EId) {
        Task task = (Task)taskMapper.selectById(TId);
        Employee employee = (Employee)employeeMapper.selectById(EId);
        task.setTStatus("已完成");
        task.setEId(EId);
        task.setFinishTime(new Timestamp(System.currentTimeMillis()));
        employee.setEStatus(true);
        taskMapper.updateById(task);
        employeeMapper.updateById(employee);
    }

    @Override
    public void updateEmployee(Employee employee) {
        employeeMapper.updateById(employee);
    }

    @Override
    public void updateEmployeeSkill(EmployeeSkill employeeSkill) {
        employeeSkillMapper.updateById(employeeSkill);
    }

    @Override
    public void updateEmployeeLogin(EmployeeLogin employeeLogin) {
        employeeLoginMapper.updateById(employeeLogin);
    }

    @Override
    public List<Task> getTasksToSelect(String EId) {//给工人选单分配

        EmployeeSkill employeeSkill = (EmployeeSkill)employeeSkillMapper.selectById(EId);
        System.out.println(employeeSkill);
        List<Task> taskList = new ArrayList<Task>();
        List<Task> tasks = null;

        if(employeeSkill.getElevator()){
            QueryWrapper<Task> queryWrapper = new QueryWrapper<Task>();
            queryWrapper.eq("skill","elevator").eq("T_status","未接单");
            tasks = taskMapper.selectList(queryWrapper);
        }
        if(tasks!=null) taskList.addAll(tasks);
        tasks = null;

        if(employeeSkill.getPrinter()){
            QueryWrapper<Task> queryWrapper = new QueryWrapper<Task>();
            queryWrapper.eq("skill","printer").eq("T_status","未接单");
            tasks = taskMapper.selectList(queryWrapper);
        }
        if(tasks!=null) taskList.addAll(tasks);
        tasks = null;

        if(employeeSkill.getComputer()){
            QueryWrapper<Task> queryWrapper = new QueryWrapper<Task>();
            queryWrapper.eq("skill","computer").eq("T_status","未接单");
            tasks = taskMapper.selectList(queryWrapper);
        }
        System.out.println(tasks);
        if(tasks!=null) taskList.addAll(tasks);
        tasks = null;

        if(employeeSkill.getAirController()){
            QueryWrapper<Task> queryWrapper = new QueryWrapper<Task>();
            queryWrapper.eq("skill","air").eq("T_status","未接单");
            tasks = taskMapper.selectList(queryWrapper);
        }
        if(tasks!=null) taskList.addAll(tasks);
        tasks = null;

        if(employeeSkill.getLight()){
            QueryWrapper<Task> queryWrapper = new QueryWrapper<Task>();
            queryWrapper.eq("skill","light").eq("T_status","未接单");
            tasks = taskMapper.selectList(queryWrapper);
        }

        if(tasks!=null) taskList.addAll(tasks);
        tasks=null;
        QueryWrapper<Task> queryWrapper = new QueryWrapper<Task>();
        List<String> stringList=new ArrayList<String>();
        stringList.add("elevator");
        stringList.add("printer");
        stringList.add("computer");
        stringList.add("light");
        stringList.add("air");
        queryWrapper.notIn("skill",stringList).eq("T_status","未接单");
        tasks = taskMapper.selectList(queryWrapper);
        if(tasks!=null) taskList.addAll(tasks);

        return taskList;
    }

    @Override
    public List<Task> getMyTasks(String EId) {
        QueryWrapper<Task> queryWrapper = new QueryWrapper<Task>();
        queryWrapper.eq("E_id",EId);
        List<Task> taskList = taskMapper.selectList(queryWrapper);
        return taskList;
    }

    @Override
    public String getTask(String TId,String EId,String material) {//工人接单
        Task task = (Task)taskMapper.selectById(TId);
        Employee employee = (Employee)employeeMapper.selectById(EId);
        if (employee.getEStatus()){
            task.setTStatus("已接单");
            task.setEId(EId);
            task.setCatchTime(new Timestamp(System.currentTimeMillis()));
            task.setMaterial(material);
            if(!material.isEmpty()){
                String[] materials = material.split(",");
                for(int i = 0;i<materials.length;i++){
                    String[] strings = materials[i].split(":");
                    Record record=new Record();
                    Integer k= recordMapper.selectCount(null)+1000;
                    record.setRId(k.toString());
                    record.setType(strings[0]);
                    record.setQuantity(Integer.parseInt(strings[1]));
                    record.setTId(TId);
                    record.setEId(EId);
                    record.setIsPass(false);
                    recordMapper.insert(record);

                }
            }
            employee.setEStatus(false);
            taskMapper.updateById(task);
            employeeMapper.updateById(employee);
            return "SUCCESS";
        }
        else return "ERROR";
    }

    public String requestout(String TId, String type) {//工人申请

        Task task=taskMapper.selectById(TId);
        task.setTStatus("已接单");
        String y=task.getMaterial();
        task.setMaterial(y+","+type);
        taskMapper.updateById(task);
        return "SUCCEED";

    }

    @Override
    public Employee getEmployee(String EId) {
        return employeeMapper.selectById(EId);
    }

    @Override
    public EmployeeSkill getEmployeeSkill(String EId) {
        return employeeSkillMapper.selectById(EId);
    }

    @Override
    public Task getTaskbyId(String TId) {
        return taskMapper.selectById(TId);
    }

    @Override
    public EmployeeLogin getEmployeeLogin(String EId) {
        return employeeLoginMapper.selectById(EId);
    }

    @Override
    public int rank(String EId) {
        QueryWrapper<Employee> queryWrapper=new QueryWrapper<Employee>();
        queryWrapper.orderByDesc("E_credit");
        List<Employee> employeeList=employeeMapper.selectList(queryWrapper);
        int i;
        for(i=0;i<employeeList.size();i++){
            if(employeeList.get(i).getEId().equals(EId)){
                break;
            }
        }
        return i+1;
    }

    @Override
    public Task getATask(String EId) {
        QueryWrapper<Task> queryWrapper = new QueryWrapper<Task>();
        queryWrapper.eq("E_id",EId).eq("T_status","申请通过").last("limit 1");
        if(taskMapper.selectList(queryWrapper).size()!=0)
        return taskMapper.selectList(queryWrapper).get(0);
        else return null;
    }
}
